/*
 * EditarAgenda.java
 *
 * Created on 30/04/2011, 14:07:11
 */

package pbf.dialog;

import calendario.InterFaceCalendario;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import org.hibernate.Session;
import pbf.conf.hibernate.HibernateUtil;
import pbf.conf.hibernate.entidades.Agendamento;
import pbf.conf.hibernate.entidades.DescricaoDoAtendimento;
import pbf.util.DataHora;
import pbf.util.Mensagem;

/**
 *
 * @author Ricardo
 */
public class AlterarAgendamento extends javax.swing.JDialog {
    private Agendamento agendamento;
    private ButtonGroup grupo = new ButtonGroup();
    private ArrayList<DescricaoDoAtendimento> listaDescricao;

    public AlterarAgendamento(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        grupo.add(botaoNao);
        grupo.add(botaoSim);
        setDescricaoAtendimento();
    }

    private void setFormulario()
    {
        int index=0;

        botaoSim.setSelected((agendamento.getAgendamentoAtPreferencial().equals("S")? true: false));
        agendamentoDtAtendimento.setText(DataHora.getData(agendamento.getAgendamentoDtAtendimento()));
        agendamentoObservacao.setText(agendamento.getAgendamentoObservacao());
        protocolo.setText(""+agendamento.getAgendamentoId());

        /*Percorre o array até encontrar um objeto com o mesmo ID de agendamento*/
        for(DescricaoDoAtendimento d : listaDescricao){
            System.out.println("ID LIST= "+d.getDtId()+" ID CLASS= "+agendamento.getDescricaoDoAtendimento().getDtId());
            if(d.getDtId().equals(agendamento.getDescricaoDoAtendimento().getDtId())){
        /*Quando encontrar ele irá para na posição exata de onde foi encontrado*/
        descricaoDoAtendimentoDtDescricao.setSelectedIndex(index);
               break;
            }
            else{
                index++;
            }
        }
    }

    private void setDescricaoAtendimento() {
        descricaoDoAtendimentoDtDescricao.removeAllItems();
        Session sessao = HibernateUtil.openSession();
        sessao.beginTransaction();

        listaDescricao = (ArrayList<DescricaoDoAtendimento>) sessao.createQuery("From pbf.conf.hibernate.entidades.DescricaoDoAtendimento ORDER BY DT_ID").list();

        for (DescricaoDoAtendimento l : listaDescricao) {
            descricaoDoAtendimentoDtDescricao.addItem(l.getDtDescricao());
        }
        sessao.getTransaction().commit();
        sessao.close();
    }

    public void setAgendamento(Agendamento agendamento){
        this.agendamento = agendamento;
        setFormulario();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        descricaoDoAtendimentoDtDescricao = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        agendamentoObservacao = new javax.swing.JTextArea();
        botaoCalendario = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        botaoSim = new javax.swing.JToggleButton();
        botaoNao = new javax.swing.JToggleButton();
        jSeparator3 = new javax.swing.JSeparator();
        jButton1 = new javax.swing.JButton();
        jLabel17 = new javax.swing.JLabel();
        agendamentoDtAtendimento = new javax.swing.JLabel();
        protocolo = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel7.setText("Atendimento Nº:");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(330, 30, 130, 20);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel8.setText("Atendimento preferêncial: ");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(20, 120, 220, 20);

        descricaoDoAtendimentoDtDescricao.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(descricaoDoAtendimentoDtDescricao);
        descricaoDoAtendimentoDtDescricao.setBounds(20, 90, 360, 23);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel9.setText("Observações:");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(20, 150, 470, 20);

        agendamentoObservacao.setColumns(20);
        agendamentoObservacao.setFont(new java.awt.Font("Arial", 0, 16));
        agendamentoObservacao.setRows(5);
        jScrollPane1.setViewportView(agendamentoObservacao);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(14, 170, 490, 60);

        botaoCalendario.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        botaoCalendario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/calendar-month.png"))); // NOI18N
        botaoCalendario.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        botaoCalendario.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        botaoCalendario.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        botaoCalendario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botaoCalendarioMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                botaoCalendarioMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                botaoCalendarioMouseExited(evt);
            }
        });
        getContentPane().add(botaoCalendario);
        botaoCalendario.setBounds(280, 20, 30, 30);

        jLabel14.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel14.setText("Descrição do Atendimento: ");
        getContentPane().add(jLabel14);
        jLabel14.setBounds(20, 60, 330, 20);

        botaoSim.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/thumb-up.png"))); // NOI18N
        botaoSim.setText("Sim");
        botaoSim.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoSim.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
        getContentPane().add(botaoSim);
        botaoSim.setBounds(270, 120, 110, 25);

        botaoNao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/thumb.png"))); // NOI18N
        botaoNao.setSelected(true);
        botaoNao.setText("Não");
        botaoNao.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        botaoNao.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        botaoNao.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
        getContentPane().add(botaoNao);
        botaoNao.setBounds(390, 120, 110, 25);
        getContentPane().add(jSeparator3);
        jSeparator3.setBounds(0, 240, 520, 10);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/toggle-expand.png"))); // NOI18N
        jButton1.setText("Outros");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(390, 90, 110, 25);

        jLabel17.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel17.setText("Data do agendamento:");
        getContentPane().add(jLabel17);
        jLabel17.setBounds(20, 30, 170, 20);

        agendamentoDtAtendimento.setFont(new java.awt.Font("Tahoma", 0, 16));
        agendamentoDtAtendimento.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        agendamentoDtAtendimento.setText("12/05/2011");
        getContentPane().add(agendamentoDtAtendimento);
        agendamentoDtAtendimento.setBounds(190, 30, 90, 20);

        protocolo.setFont(new java.awt.Font("Tahoma", 1, 16));
        protocolo.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        protocolo.setText("9999");
        protocolo.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        getContentPane().add(protocolo);
        protocolo.setBounds(450, 30, 50, 20);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/date_edit.png"))); // NOI18N
        jButton2.setText("Editar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(270, 250, 110, 25);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/cross-circle.png"))); // NOI18N
        jButton3.setText("Cancelar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(390, 250, 110, 25);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-539)/2, (screenSize.height-330)/2, 539, 330);
    }// </editor-fold>//GEN-END:initComponents

    private void botaoCalendarioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoCalendarioMouseClicked
        InterFaceCalendario calendario = new InterFaceCalendario(null, true);
        calendario.setVisible(true);
        agendamentoDtAtendimento.setText(calendario.getData());
}//GEN-LAST:event_botaoCalendarioMouseClicked

    private void botaoCalendarioMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoCalendarioMouseEntered
        botaoCalendario.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
}//GEN-LAST:event_botaoCalendarioMouseEntered

    private void botaoCalendarioMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoCalendarioMouseExited
        botaoCalendario.setBorder(null);
}//GEN-LAST:event_botaoCalendarioMouseExited

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        CadastrarAtendimentoReferente cadastrarAtendimento = new CadastrarAtendimentoReferente(null, true);
        cadastrarAtendimento.setVisible(true);
        setDescricaoAtendimento();
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        dispose();
}//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try{
        Session sessao = HibernateUtil.openSession();
        sessao.beginTransaction();
        agendamento.setAgendamentoAtPreferencial((botaoSim.isSelected())? "S":"N");
        agendamento.setAgendamentoDtAtendimento(DataHora.getDate(agendamentoDtAtendimento.getText()));
        agendamento.setAgendamentoObservacao(agendamentoObservacao.getText());
        agendamento.setDescricaoDoAtendimento(listaDescricao.get(descricaoDoAtendimentoDtDescricao.getSelectedIndex()));
        sessao.update(agendamento);
        Mensagem.mensageiro("Agendamento Alterado com sucesso!");
        sessao.getTransaction().commit();
        sessao.close();
        dispose();
        }catch(Exception e){
            System.out.println("Este problema foi isolado porque mesmo com isso ele funciona corretamente[AlterarAgendamento:Evento do Botão Editar]: "+e);
            Mensagem.mensageiro("Agendamento Alterado com sucesso!");
            dispose();
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AlterarAgendamento dialog = new AlterarAgendamento(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel agendamentoDtAtendimento;
    private javax.swing.JTextArea agendamentoObservacao;
    private javax.swing.JLabel botaoCalendario;
    private javax.swing.JToggleButton botaoNao;
    private javax.swing.JToggleButton botaoSim;
    private javax.swing.JComboBox descricaoDoAtendimentoDtDescricao;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JLabel protocolo;
    // End of variables declaration//GEN-END:variables

}
